package com.example.aqarapp.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;


import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.example.aqarapp.entity.BaseDeviceInfo;
import com.example.aqarapp.entity.BulbInfo;
import com.example.aqarapp.entity.CurtainInfo;
import com.example.aqarapp.entity.DeviceInfo;
import com.example.aqarapp.entity.DeviceListItem;
import com.example.aqarapp.entity.FamilyInfo;
import com.example.aqarapp.entity.OneBean;
import com.example.aqarapp.entity.RoomInfo;
import com.example.aqarapp.entity.SocketInfo;
import com.example.aqarapp.model.FamilyInfoModel;

import java.util.ArrayList;
import java.util.List;

import static com.example.aqarapp.view.MainPageActivity.curFamilyName;
import static com.example.aqarapp.view.MainPageActivity.curRoomName;


public class FamilyInfoViewModel extends ViewModel {

    private  List<FamilyInfo> familyInfoList;
    private FamilyInfoModel mFamilyInfoModel;
    private MutableLiveData<List<String>> mFamilyInfo;
    private MutableLiveData<List<DeviceInfo>> mDeviceInfo;
    private List<RoomInfo> roomInfoList = new ArrayList<>();
    private List<String> roomNameList = new ArrayList<>();
    private List<DeviceInfo> quickDeviceList = new ArrayList<>();
    private ArrayList<DeviceInfo> deviceInfoList;
    private  MutableLiveData<List<OneBean>> mDeviceListInfo;
    private static String GATEWAY_CLOSED = "0";


    public FamilyInfoViewModel() {
        Log.d("liyao1","FamilyInfoViewModel");
        mFamilyInfoModel = new FamilyInfoModel();
        familyInfoList = mFamilyInfoModel.getFamilyInfo();
    }

    public LiveData<List<String>> getRoomNameInfo() {
        if (mFamilyInfo == null) {
            mFamilyInfo = new MutableLiveData<>();
        }
        return mFamilyInfo;
    }



    public LiveData<List<DeviceInfo>> getDeviceInfo() {
        if (mDeviceInfo == null) {
            mDeviceInfo = new MutableLiveData<>();
        }
        return mDeviceInfo;
    }

    public LiveData<List<OneBean>> getDeviceListInfo() {
        if (mDeviceListInfo == null) {
            mDeviceListInfo = new MutableLiveData<>();
        }
        return mDeviceListInfo;
    }


    //获取设备列表模式下对应location的设备信息
    public DeviceListItem getDeviceListItem1(String roomName){
        deviceInfoList = new ArrayList<>();
        DeviceListItem deviceListItem = new DeviceListItem();
        for (int i=0;i<familyInfoList.size();i++){
            if(familyInfoList.get(i).familyName.equals(curFamilyName)){
                for(int j=0;j<familyInfoList.get(i).getRoomInfoList().size();j++){
                    if(familyInfoList.get(i).getRoomInfoList().get(j).roomName.equals(roomName)){
                        Log.d("liyao1"," getRoomNameList roomName="+roomName);
                        deviceInfoList.addAll(familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList());
                        for(int k=0;k<deviceInfoList.size();k++){
                            //灯泡
                            String location =  deviceInfoList.get(k).getBaseDeviceInfo().getLocation();//这里DeviceList和baseDeviceInfo都包含了location。
                            if(deviceInfoList.get(k).model.equals("lumi.light.aqcn02")){
                                String bulbColorValue =((BulbInfo) deviceInfoList.get(k).getBaseDeviceInfo()).getColortemperature();
                                String bulbStateValue = ((BulbInfo) deviceInfoList.get(k).getBaseDeviceInfo()).getBulbStatus();
                                String bulbLightValue = ((BulbInfo) deviceInfoList.get(k).getBaseDeviceInfo()).getBulbLight();
                                deviceListItem.setBulbInfo(new BulbInfo(location,bulbLightValue,bulbStateValue,bulbColorValue));
                            }else if(deviceInfoList.get(k).model.equals("lumi.curtain.v1")){
                                String curtainStateValue = ((CurtainInfo) deviceInfoList.get(k).getBaseDeviceInfo()).getCurtainStatus();
                                deviceListItem.setCurtainInfo(new CurtainInfo(location, curtainStateValue));
                            }else if(deviceInfoList.get(k).model.equals("lumi.plug.v1")){
                                String socketStateValue = ((SocketInfo) deviceInfoList.get(k).getBaseDeviceInfo()).getSocketState();
                                deviceListItem.setSocketInfo(new SocketInfo(location, socketStateValue));
                            }
                            deviceListItem.setLocation(location);
                        }
                    }
                }
            }
        }
        return deviceListItem;
    }

    //获取设备列表模式下对应location的设备信息
    public List<OneBean> getAllDeviceListItem(String placeName) {
        List<OneBean> oneBeanList = new ArrayList<>();
        List<BaseNode> baseNodeList = new ArrayList<>();
        for (int i = 0; i < familyInfoList.size(); i++) {
            if (familyInfoList.get(i).familyName.equals(curFamilyName)) {
                for (int j = 0; j < familyInfoList.get(i).getRoomInfoList().size(); j++) {
                    if (placeName.equals("全部")) {
                        baseNodeList.clear();
//                      String roomId = familyInfoList.get(i).getRoomInfoList().get(j).getRoomId();
                        String roomName = familyInfoList.get(i).getRoomInfoList().get(j).getRoomName();
                        baseNodeList.addAll(familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList());
                        OneBean oneBean = new OneBean(new ArrayList<>(baseNodeList), roomName);
                        oneBeanList.add(oneBean);
                    } else if (familyInfoList.get(i).getRoomInfoList().get(j).roomName.equals(placeName)) {
                        baseNodeList.clear();
                        baseNodeList.addAll(familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList());
                        OneBean oneBean = new OneBean(new ArrayList<>(baseNodeList), placeName);
                        oneBeanList.add(oneBean);
                    }
                }
            }
        }
        return oneBeanList;
    }



    //获取家庭名称
    public List<String> getFamilyName(){
        List<String> familyList = new ArrayList<>();
        for(int i = 0;i<familyInfoList.size();i++){
            familyList.add(familyInfoList.get(i).familyName);
        }
        return  familyList;
    }

    //获取房间名称
    public List<String> getRoomNameList(String familyName){
        roomInfoList.clear();
        roomNameList.clear();
        for(int i = 0;i<familyInfoList.size();i++){
            if(familyInfoList.get(i).familyName.equals(familyName)){
                Log.d("liyao1"," familyName.equals(familyName)"+familyName +"i="+i);
                Log.d("liyao1"," familyInfoList size="+familyInfoList.size());
                roomInfoList.addAll (familyInfoList.get(i).getRoomInfoList());
                Log.d("liyao1"," roomInfoList.size="+familyInfoList.get(i).getRoomInfoList().size());
                for (int j=0 ;j<roomInfoList.size();j++){
                    roomNameList.add(roomInfoList.get(j).roomName);
                }
            }
        }
        return roomNameList;
    }

    //获取快捷模式当前location下所有设备信息
    public List<DeviceInfo> getQuickDeviceItem(String roomName){
        quickDeviceList.clear();
        for (int i=0;i<familyInfoList.size();i++){
            if(familyInfoList.get(i).familyName.equals(curFamilyName)){
                for(int j=0;j<familyInfoList.get(i).getRoomInfoList().size();j++){
                    if(familyInfoList.get(i).getRoomInfoList().get(j).roomName.equals(roomName)){
                        Log.d("liyao1"," getRoomNameList roomName="+roomName);
                        quickDeviceList.addAll(familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList());
                    }
                }
            }
        }
        return quickDeviceList;
    }
    //刷新 获取当前family下所有设备信息  重新获取所有数据并更新到本地缓存，同时更新当前family的UI数据
    public void RefreshDeviceList() {
        familyInfoList = mFamilyInfoModel.getFamilyInfo();
    }

    //获取网关关闭的设备列表
    public List<OneBean> getDeviceListGatewayItem(String placeName) {
        List<DeviceInfo> deviceInfoList = new ArrayList<>();
        List<OneBean> oneBeanList = new ArrayList<>();
        List<BaseNode> baseNodeList = new ArrayList<>();
        for (int i = 0; i < familyInfoList.size(); i++) {
            if (familyInfoList.get(i).familyName.equals(curFamilyName)) {
                for (int j = 0; j < familyInfoList.get(i).getRoomInfoList().size(); j++) {
                    baseNodeList.clear();
                    deviceInfoList.clear();
                    if (placeName.equals("全部")) {
//                      String roomId = familyInfoList.get(i).getRoomInfoList().get(j).getRoomId();
                        String roomName = familyInfoList.get(i).getRoomInfoList().get(j).getRoomName();
                        for(int k =0 ;k<familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList().size();k++) {
                            DeviceInfo deviceInfo = familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList().get(k);
                            if (deviceInfo.getState().equals(GATEWAY_CLOSED)) {
                                deviceInfoList.add(deviceInfo);
                            }
                        }
                        baseNodeList.addAll(deviceInfoList);
                        OneBean oneBean = new OneBean(new ArrayList<>(baseNodeList), roomName);
                        oneBeanList.add(oneBean);
                    } else if (familyInfoList.get(i).getRoomInfoList().get(j).roomName.equals(placeName)) {
                        for(int k =0 ;k<familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList().size();k++) {
                            DeviceInfo deviceInfo = familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList().get(k);
                            if (deviceInfo.getState().equals(GATEWAY_CLOSED)) {
                                deviceInfoList.add(deviceInfo);
                            }
                        }
                        baseNodeList.addAll(deviceInfoList);
                        OneBean oneBean = new OneBean(new ArrayList<>(baseNodeList), placeName);
                        oneBeanList.add(oneBean);
                    }
                }
            }
        }
        return oneBeanList;
    }

    public <T> List<OneBean> getDeviceSpecifiedTypeItem(String placeName, Class<T> t) {
        List<DeviceInfo> deviceInfoList = new ArrayList<>();
        List<OneBean> oneBeanList = new ArrayList<>();
        List<BaseNode> baseNodeList = new ArrayList<>();
        for (int i = 0; i < familyInfoList.size(); i++) {
            if (familyInfoList.get(i).familyName.equals(curFamilyName)) {
                for (int j = 0; j < familyInfoList.get(i).getRoomInfoList().size(); j++) {
                    baseNodeList.clear();
                    deviceInfoList.clear();
                    if (placeName.equals("全部")) {
//                      String roomId = familyInfoList.get(i).getRoomInfoList().get(j).getRoomId();
                        String roomName = familyInfoList.get(i).getRoomInfoList().get(j).getRoomName();
                        for(int k =0 ;k<familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList().size();k++) {
                            DeviceInfo deviceInfo = familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList().get(k);
                            if (deviceInfo.getBaseDeviceInfo().getClass().equals(t)) {
                                deviceInfoList.add(deviceInfo);
                            }
                        }
                        baseNodeList.addAll(deviceInfoList);
                        OneBean oneBean = new OneBean(new ArrayList<>(baseNodeList), roomName);
                        oneBeanList.add(oneBean);
                    } else if (familyInfoList.get(i).getRoomInfoList().get(j).roomName.equals(placeName)) {
                        for(int k =0 ;k<familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList().size();k++) {
                            DeviceInfo deviceInfo = familyInfoList.get(i).getRoomInfoList().get(j).getDeivceInfoList().get(k);
                            if (deviceInfo.getBaseDeviceInfo().getClass().equals(t)) {
                                deviceInfoList.add(deviceInfo);
                            }
                        }
                        baseNodeList.addAll(deviceInfoList);
                        OneBean oneBean = new OneBean(new ArrayList<>(baseNodeList), placeName);
                        oneBeanList.add(oneBean);
                    }
                }
            }
        }
        return oneBeanList;
    }


//    public List<MultiItemEntity> getDeviceListItem(){
//        OneBean oneBean  = new OneBean(curRoomName);
//        List<DeviceInfo> quickDeviceItem = getQuickDeviceItem(curRoomName);
//        for(int i =0 ;i<quickDeviceItem.size();i++){
//            oneBean.addSubItem(quickDeviceItem.get(i));
//        }
//
//    }
}
